function bindLogoImg() {
    $("#logo_uploadimg").uploadify({
        uploader: ctx + "/js/uploadify/uploadify.swf?var=" + (new Date()).getTime(),
        script: ctx + "/json/media/uploadlogopic;jsessionid=" + jsessionid,
        scriptData: {},
        cancelImg: ctx + "/js/uploadify/cancel.png",
        buttonImg: imagePath + "picup_btn.gif",
        height: 35,
        folder: "/uploads",
        fileExt: "*.jpg;*.gif;*.png",
        fileDesc: "Image Files(*.jpg, *.gif, *.png)",
        fileDataName: "uploadfile",
        sizeLimit: 2 * 1024 * 1024,
        queueID: "replacing_area_pic",
        onProgress: function(a) {
            moveLogoDiv(2);
            logouploading = 1;
            showLogoUploading()
        },
        onComplete: function(e, a, d, b, f) {
            logouploading = 0;
            logoimgloadflag = 1;
            showLogoimgload();
            logosuccessflag = false;
            if (b != "" && b.indexOf("totalSizeLimit") > 0) {
                tips("抱歉，超过最大容量限制！", "error", null, 2005);
                return false
            } else {
                var c = $.evalJSON(b);
                if (c.error == "0") {
                    moveLogoDiv(3);
                    showJcropImg(c.msg)
                } else {
                    tips(" 上传出错，请重新上传！", "error", null, 2005);
                    return false
                }
            }
        },
        onError: function(d, a, c, b) {
            if (b.type == "File Size") {
                tips("抱歉，图片文件应小于2MB！", "error", null, 2005);
                return false
            }
        },
        auto: true
    })
}
function bindLogoLoadSuccess() {
    $("#jcroplogofile").load(function() {
        logosuccessflag = true
    })
}
function bindLogoRemote() {
    $("#logoremoteimg").load(function() {
        $("#logoEditDIV").trigger("click")
    })
}
function showlogoUpload() {
    $("#logopreviewdiv").removeClass("edit-pic");
    $("#logopreview").html('<div class="initial">企业logo</div>').css("width", 120).css("height", 40).show();
    $("#jcroplogofile").attr("src", "");
    if (jcropimg != null) {
        jcropimg.destroy()
    }
    moveLogoDiv(1)
}
function moveLogoDiv(a) {
    var b = $("#uploadlogo" + a).height();
    $("#uploadlogoDIV").css("margin-left", (1 - a) * 240 + "px");
    if ($("#logopreviewimg").height() > b) {
        b = $("#logopreviewimg").height() + 20
    }
    $("#uploadlogoDIV").css("height", b);
    $("#logoEditDIV").trigger("click")
}
function showLogoUploading() {
    if (logouploading > 0) {
        var a = ".";
        var c = 1000;
        for (var b = 0; b < (logouploading % 5); b++) {
            a += "."
        }
        logouploading++;
        $("#showuploadingmsg").val("图片上传中" + a);
        setTimeout(function() {
            showLogoUploading()
        },
        c)
    }
}
function showLogoimgload() {
    logouploading = 0;
    if (logoimgloadflag > 0) {
        var a = ".";
        var c = 1000;
        for (var b = 0; b < (logoimgloadflag % 5); b++) {
            a += "."
        }
        logoimgloadflag++;
        $("#showuploadingmsg").val("图片加载中" + a);
        setTimeout(function() {
            showLogoimgload()
        },
        c)
    }
}
var jcropimgurl = "";
var jcropwidth = 120;
var jcropheight = 40;
var jcropimg = null;
var logouploading = 0;
var logoimgloadflag = 0;
var logosuccessflag = false;
var logoWidth = 0;
var logoHeight = 0;
function showJcropImg(a) {
    $("#jcroplogofileBox").html($('<img id="jcroplogofile" src="' + ctx + "/" + a + '">'));
    bindLogoLoadSuccess();
    $("#logopreviewdiv").addClass("edit-pic");
    $("#logopreview").html('<img id="logopreviewimg" src="' + ctx + "/" + a + '">').hide();
    $("#logodelete").remove();
    checkJcropImg()
}
function checkJcropImg() {
    if (logosuccessflag) {
        logoimgloadflag = 0;
        initJcrop()
    } else {
        setTimeout(function() {
            checkJcropImg()
        },
        100)
    }
}
function initJcrop() {
    logoWidth = $("#jcroplogofile").width();
    logoHeight = $("#jcroplogofile").height();
    if (jcropimg != null) {
        jcropimg.destroy()
    }
    jcropimg = $.Jcrop("#jcroplogofile", {
        boxWidth: 230,
        boxHeight: 153,
        onChange: showCoords,
        onSelect: showCoords,
        setSelect: [0, 0, $("#jcroplogofile").width(), $("#jcroplogofile").height()]
    });
    $("#logopreview").show()
}
function showCoords(e) {
    var a = 120;
    var b = 160;
    var g = 120;
    var d = 40;
    $("#filex1").val(e.x);
    $("#filey1").val(e.y);
    $("#filex2").val(e.x2);
    $("#filey2").val(e.y2);
    $("#filew").val(e.w);
    $("#fileh").val(e.h);
    if (parseInt(e.w) > 0) {
        if (parseInt(e.w) > a || parseInt(e.h) > b) {
            g = parseInt(e.w);
            d = parseInt(e.h);
            if (g > a) {
                d = d * a / g;
                g = a
            }
            if (d > b) {
                g = g * b / d;
                d = b
            }
        } else {
            g = parseInt(e.w);
            d = parseInt(e.h)
        }
        $("#logopreview").css("width", g).css("height", d).show();
        var f = g / e.w;
        var c = d / e.h;
        $("#logopreviewimg").css({
            width: Math.round(f * logoWidth) + "px",
            height: Math.round(f * logoHeight) + "px",
            marginLeft: "-" + Math.round(f * e.x) + "px",
            marginTop: "-" + Math.round(c * e.y) + "px"
        })
    } else {
        $("#logopreview").css("width", 0).css("height", 0).hide()
    }
}
function saveLogo() {
    if (parseInt($("#filew").val(), 10) <= 0 || parseInt($("#fileh").val(), 10) <= 0) {
        tips("请拖动鼠标裁切图片显示区域", "error", null, 10000);
        return
    }
    var a = parseInt($("#filex1").val(), 10);
    var c = parseInt($("#filex2").val(), 10);
    var f = parseInt($("#filey1").val(), 10);
    var d = parseInt($("#filey2").val(), 10);
    var e = parseInt($("#fileh").val(), 10);
    var b = parseInt($("#filew").val(), 10);
    if (a < 0) {
        if ((c - a) >= $("#jcroplogofile").width()) {
            c = $("#jcroplogofile").width()
        } else {
            c -= a
        }
        a = 0
    }
    if (f < 0) {
        if ((d - f) >= $("#jcroplogofile").height()) {
            d = $("#jcroplogofile").height()
        } else {
            d -= f
        }
        f = 0
    }
    if (savelogoshowflag > 0) {
        return
    }
    savelogoshowflag = 1;
    checkSaveLogoShow();
    ajaxPost(ctx + "/json/media/savelogo", {
        url: $("#jcroplogofile").attr("src"),
        filex1: a,
        filey1: f,
        filex2: c,
        filey2: d,
        filew: b,
        fileh: e
    },
    function(g) {
        if (g != null && g.error == "0" && g.msg != "") {
            if (savelogoshowflag > 0) {
                saveSurveyLogourl(g.msg);
                savelogoshowflag = 0;
                $("#savelogoButton").html("&nbsp;确定&nbsp;")
            }
        } else {}
    })
}
var savelogoshowflag = 0;
function checkSaveLogoShow() {
    if (savelogoshowflag > 0) {
        var a = ".";
        var c = 1000;
        for (var b = 0; b < (savelogoshowflag % 5); b++) {
            a += "."
        }
        savelogoshowflag++;
        $("#savelogoButton").html("&nbsp;处理中" + a + "&nbsp;");
        setTimeout(function() {
            checkSaveLogoShow()
        },
        c)
    }
}
function cancelSaveLogo() {
    savelogoshowflag = 0;
    showlogoUpload()
}
function changeRemoteUrl() {
    if ($("#logoremoteurl").val() == "") {
        $("#logoremote2").hide();
        $("#logoremote1").show()
    } else {
        $("#logoremoteimg").attr("src", $("#logoremoteurl").val());
        $("#logoremote1").hide();
        $("#logoremote2").show()
    }
}
function saveLogoRemote() {
    saveSurveyLogourl($("#logoremoteurl").val())
}
function saveSurveyLogourl(b) {
    editFlag = true;
    var c = encodeURI(b);
    var a = decodeURI(b);
    if (b == a) {
        survey.logo = c
    } else {
        survey.logo = b
    }
    $("#showlogo").removeClass("logo-add");
    $("#showlogo a").html('<img class="img120" src="' + (survey.logo.indexOf("http://") < 0 ? ctx + "/": "") + survey.logo + '">');
    closeFloatLayer("#logoEditDIV")
}
function removeSurveyLogo() {
    artconfirm("确认删除？", 
    function() {
        editFlag = true;
        survey.logo = "";
        $("#logopreview").html('<div class="initial">企业logo</div>').css("width", 120).css("height", 40).show();
        $("#logodelete").remove();
        $("#showlogo").addClass("logo-add");
        $("#showlogo a").html('<img width="16" height="16" src="' + imagePath + 'add.png">添加企业LOGO');
        closeFloatLayer("#logoEditDIV")
    },
    function() {},
    "确定", "取消", null, 10000)
};